<?php
namespace library\widgets;

use idea\mvc\Widget;

/**
 * TableBuilder class file
 * 创建表格
 * @author 宋欢 <iphper@yeah.net>
 * @version $Id: TableBuilder.php 1 2013-04-20 17:11:06Z huan.song $
 * @package library.widgets
 * @since 1.0
 */
class TableBuilder extends Widget
{	
	/**
	 * @var string <table>的class名
	 */
	const CSS_CLASS_NAME = 'table bordered';
	
	/**
	 * @var string 首页
	 */
	const PAGE_BEGIN = '首页';
	
	/**
	 * @var string 上一页
	 */
	const PAGE_PREV = '上一页';
	
	/**
	 * @var string 下一页
	 */
	const PAGE_NEXT = '下一页';
	
	/**
	 * @var string 尾页
	 */
	const PAGE_END = '尾页';
	
	/**
	 * @var array 表单属性
	 */
	protected $_attributes = array();
	
	/**
	 * 执行Widget类，输出内容
	 * @return void
	 */
	public function run()
	{
		$this->_attributes = $this->_tplVars['form']->getAttributes();
		
		$html = "<table id=\"show_list\" class=\"" . self::CSS_CLASS_NAME . "\">\n";
		$html .= $this->getThead();
		$html .= $this->getTbody();
		$html .= $this->getTfoot();
		$html .= "<table>\n";
		echo $html;
	}
	
	/**
	 * 获取Html表格元素：<tbody></tbody>
	 * @return string
	 */
	public function getTbody()
	{
		$html = "<tbody>\n";
		foreach ($this->_tplVars['data'] as $row) {
			$html .= $this->getTr($row);
		}
		
		$html .= "</tbody>\n";
		return $html;
	}
	
	/**
	 * 获取Html表格元素：<tr></tr>
	 * @param array $data
	 * @return string
	 */
	public function getTr($data)
	{
		$html = "<tr>\n";
		foreach ($this->_tplVars['columns'] as $columnName => $columns) {
			if (is_int($columnName) && is_string($columns)) {
				$columnName = $columns;
				$columns = array();
			}
			
			if (isset($columns['name'])) {
				$columnName = $columns['name'];
			}
			
			if (isset($data[$columnName])) {
				$value = $data[$columnName];
				if (isset($columns['callback'])) {
					if (isset($columns['addonname']) && isset($data[$columns['addonname']])) {
						$value = call_user_func($columns['callback'], $value, $data[$columns['addonname']]);
					}
					else {
						$value = call_user_func($columns['callback'], $value);
					}
				}
			}
			else {
				$value = isset($columns['value']) ? $columns['value'] : '';
				if (isset($columns['replace'])) {
					$replace = $columns['replace'];
					$value = str_replace('__' . $replace . '__', $data[$replace], $value);
				}
			}
			
			$html .= "<td>" . $value . "</td>\n";
		}
		
		$html .= "</tr>\n";
		return $html;
	}
	
	/**
	 * 获取Html表格元素：<thead></thead>
	 * @return string
	 */
	public function getThead()
	{
		$html = "<thead><tr>\n";
		foreach ($this->_tplVars['columns'] as $columnName => $columns) {
			if (is_int($columnName) && is_string($columns)) {
				$columnName = $columns;
				$columns = array();
			}
			
			if (isset($columns['name'])) {
				$columnName = $columns['name'];
			}
			
			$label = isset($columns['label']) ? $columns['label'] : '';
			if ($label === '' && isset($this->_attributes[$columnName]['label'])) {
				$label = $this->_attributes[$columnName]['label'];
			}
			
			$attributes = isset($columns['attributes']) ? (array) $columns['attributes'] : array();
			$html .= $this->getHtml()->tag('td', $attributes, $label);
		}
		
		$html .= "\n</tr></thead>\n";
		return $html;
	}
	
	/**
	 * 获取Html表格元素：<tfoot></tfoot>
	 * @return string
	 */
	public function getTfoot()
	{
		if (!isset($this->_tplVars['page'])) {
			return '';
		}
		
		$links = $this->_tplVars['page']['links'];

		$html = '<tfoot><tr class="pager"><td colspan="' . count($this->_tplVars['columns']) . '">';
		$html .= $this->getHtml()->a(self::PAGE_BEGIN, $links['begin']);
		$html .= $this->getHtml()->a(self::PAGE_PREV, $links['prev']);
		foreach ($links['lists'] as $page => $href) {
			$selected = is_numeric($href) ? 'selected' : '';
			$html .= $this->getHtml()->a($page, $href, array('class' => $selected));
		}
		
		$html .= $this->getHtml()->a(self::PAGE_NEXT, $links['next']);
		$html .= $this->getHtml()->a(self::PAGE_END, $links['end']);
		$html .= '</td></tr></tfoot>';
		
		return $html;
	}
}
